package com.zf.nospring.rabbitmq.alternate_exchange;

import com.rabbitmq.client.*;
import com.zf.nospring.rabbitmq.utils.RabbitUtils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 消费正常队列和备份交换机绑定的队列
 */
public class AlternateConsumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = RabbitUtils.getDefaultConnection();
        Channel channel1 = connection.createChannel();
        Channel channel2 = connection.createChannel();

        // 消费正常队列的消费逻辑
        DefaultConsumer consumer1 = new DefaultConsumer(channel1) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.printf("正常消息：envelope: %s, msg: %s\n", envelope, new String(body));
            }
        };

        // 消费正常队列的消费逻辑
        DefaultConsumer consumer2 = new DefaultConsumer(channel2) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.printf("警告：发现不可路由消息：envelope: %s, msg: %s\n", envelope, new String(body));
            }
        };

        // 消费正常队列
        channel1.basicConsume(AlternateProducer.testAlterNormalQueue, true, consumer1);
        // 消费备份交换机绑定的队列
        channel2.basicConsume(AlternateProducer.testAlternateQueue, true, consumer2);
    }
}
